import { _decorator, Component, Node,Vec3 } from 'cc';
import { loading } from './loading';
import { UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('backGround')
export class backGround extends Component {
    @property(Node)
    public bg1:Node = null
    @property(Node)
    public bg2:Node = null
    // 移动速度
    @property(Number)
    public speed:number = 0

    update(deltaTime: number) {
      // 游戏不是失败状态才移动 
      if(loading.gameState){
         // 移动背景
         this.bg1.setPosition(new Vec3( this.bg1.getPosition().x - this.speed * deltaTime ,this.bg1.getPosition().y, 0))
         this.bg2.setPosition(new Vec3( this.bg2.getPosition().x - this.speed * deltaTime ,this.bg2.getPosition().y, 0))
         
         // 检查是否超出屏幕
         const bgWidth = this.bg1.getComponent(UITransform).contentSize.width * this.bg1.scaleX
         if (this.bg1.getPosition().x <= -bgWidth) {
            this.bg1.setPosition(new Vec3( this.bg2.getPosition().x + bgWidth ,this.bg1.getPosition().y, 0))
            //  this.bg1.getPosition().x = this.bg2.getPosition().x + bgWidth;
         }
         if (this.bg2.getPosition().x <= -bgWidth) {
            this.bg2.setPosition(new Vec3( this.bg1.getPosition().x + bgWidth ,this.bg1.getPosition().y, 0))
            //  this.ground2.getPosition().x = this.ground1.getPosition().x + bgWidth;
         }
      }
    }
}


